package svm_oao;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import data_access.ResultObj;
import libsvm.svm;
import libsvm.svm_model;
import libsvm.svm_parameter;

public class SVMUsage {
	private static String error_message;
	private static String accturacy;
	private static long time;

	public static String getError_message() {
		return error_message;
	}

	public static String getAccturacy() {
		return accturacy;
	}

	public static long getTime() {
		return time;
	}

	public static String trainData(File file) throws IOException {
		long start, end;
		start = System.currentTimeMillis();

		svm_parameter param = new svm_parameter();
		// default values
		param.svm_type = svm_parameter.C_SVC;
		param.kernel_type = svm_parameter.RBF;
		param.degree = 3;
		param.gamma = 0; // 1/num_features
		param.coef0 = 0;
		param.nu = 0.5;
		param.cache_size = 100;
		param.C = 1;
		param.eps = 1e-3;
		param.p = 0.1;
		param.shrinking = 1;
		param.probability = 0;
		param.nr_weight = 0;
		param.weight_label = new int[0];
		param.weight = new double[0];

		String input_file_name = file.getAbsolutePath();
		String name = file.getName();
		int pos = name.lastIndexOf(".");
		if (pos > 0) {
			name = name.substring(0, pos);
		}
		String model_file_name = file.getParent() + "\\" + name + ".model";

		SVMTrain svmTrain = new SVMTrain(param, input_file_name);
		svmTrain.read_problem();

		error_message = svm.svm_check_parameter(svmTrain.getProb(), param);
		if (error_message != null) {
			System.err.print("ERROR: " + error_message + "\n");
			System.exit(1);
		} else {
			svm_model model = svm.svm_train(svmTrain.getProb(), param);
			svm.svm_save_model(model_file_name, model);
		}
		end = System.currentTimeMillis();
		time = end - start;
		System.out.println("Time Train: " + time);
		return model_file_name;
	}

	public static String predictData(File file, svm_model model, int predict_probability, int type, ResultObj obj)
			throws IOException {
		long start, end;
		start = System.currentTimeMillis();
		BufferedReader input = new BufferedReader(new FileReader(file));
		String name = file.getName();
		int pos = name.lastIndexOf(".");
		if (pos > 0) {
			name = name.substring(0, pos);
		}
		String output_file_name = file.getParent() + "\\" + name + ".out";
		DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(output_file_name)));
		if (predict_probability == 1) {
			if (svm.svm_check_probability_model(model) == 0) {
				/*
				 * System.err .print(
				 * "Model does not support probabiliy estimates\n" );
				 * System.exit(1);
				 */
				predict_probability = 0;
			}
		} else {
			if (svm.svm_check_probability_model(model) != 0) {
				System.out.println("Model supports probability estimates, but disabled in prediction.\n");
			}
		}
		accturacy = SVMPredict.predict(input, output, model, predict_probability, type, obj);
		input.close();
		output.close();
		end = System.currentTimeMillis();
		time = end - start;
		System.out.println("Time Predict: " + time);
		return output_file_name;
	}
}
